﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class OA_DepartMentList : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindTree();
                btnTop.OnClientClick = Window1.GetShowReference("OA_DepartmentChange.aspx", "添加主部门");

            }
        }

        #region BindGrid

        protected void Tree1_NodeCommand(object sender, ChanExt.TreeCommandEventArgs e)
        {

            if (e.NodeID != "")
            {
                btnChild.OnClientClick = Window1.GetShowReference("OA_DepartmentChange.aspx?pid=" + e.NodeID, "添加子类别");
                btnEdit.OnClientClick = Window1.GetShowReference("OA_DepartmentChange.aspx?id=" + e.NodeID, "修改【" + e.Node.Text + "】类别");
            }
        }

        protected void Exp1(object sender, EventArgs e)
        {
            MgrTree.Collapsed = true;
        }

        protected void Exp2(object sender, EventArgs e)
        {
            MgrTree.Expanded = false;
        }


        protected void DeleteNode_Click(object sender, EventArgs e)
        {
            if (MgrTree.SelectedNodeID != "")
            {
                TbsDbSession.Default.Delete<OA_Department>(OA_Department._.DepID == MgrTree.SelectedNodeID);
                Alert.ShowInTop("删除成功！");
                BindTree();
            }
            else
            {
                Alert.ShowInTop("请选择一项进行删除！");
            }
        }

        protected void page_reload(object sender, EventArgs e)
        {
            PageContext.Refresh();
        }


        private void BindTree()
        {
            MgrTree.Nodes.Clear();
            List<OA_Department> items = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.TopID == 0).OrderBy(OA_Department._.OrderID.Asc).ToList();

            foreach (OA_Department row in items)
            {
                TreeNode node = new TreeNode();
                node.Text = row.DepName;
                node.NodeID = row.DepID.ToString();
                node.Expanded = true;
                node.EnablePostBack = true;
                MgrTree.Nodes.Add(node);
                ResolveSubTree(row.DepID, node);
            }

        }

        private void ResolveSubTree(int id, TreeNode treeNode)
        {

            List<OA_Department> items = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.TopID == id).OrderBy(OA_Department._.OrderID.Asc).ToList();

            if (items.Count > 0)
            {
                treeNode.Expanded = true;
                foreach (OA_Department row in items)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.DepName;
                    node.NodeID = row.DepID.ToString();
                    node.Expanded = true;
                    node.EnablePostBack = true;
                    treeNode.Nodes.Add(node);
                    ResolveSubTree(row.DepID, node);
                }
            }
        }




        #endregion

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            //int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            //if (e.CommandName == "Del")
            //{

            //    int c = MySoft.DbSession.Default.From<Tbs_Modules>().Where(Tbs_Modules._.Topmid == id).Count();
            //    if (c == 0)
            //    {
            //        MySoft.DbSession.Default.Delete<Tbs_Modules>(Tbs_Modules._.Mid == id);
            //        Alert.ShowInTop("删除成功！");
            //        BindGrid();
            //    }
            //    else
            //    {
            //        Alert.ShowInTop("该模块下还存在子模块，请先把子模块删除！");
            //    }

            //}
        }

        protected void btnSetPower_Click(object sender, EventArgs e)
        {
            if (MgrTree.SelectedNodeID != "")
            {
                int fatherid = int.Parse(MgrTree.SelectedNodeID);
                //int c = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.TopID == fatherid).Count();
                //if (c > 0)
                //    Alert.Show("该部门下还有子部门，只能设定最底层的子部门！");
                //else
                PageContext.RegisterStartupScript(Window3.GetShowReference("SysUserSetPer.aspx?uid=" + fatherid + "&mtype=1"));
                //if()
            }
            else
                Alert.Show("请选择要设定的部门");
        }
    }
}